package com.youlu.campus.admin.activity.controller;

import com.youlu.campus.activity.entity.UserLotteryRecord;
import com.youlu.campus.activity.entity.UserLotteryRecordExcel;
import com.youlu.campus.activity.entity.UserLotteryRecordQueryVo;
import com.youlu.campus.activity.service.UserLotteryRecordService;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 抽奖记录
 *
 * @Description: ActivityLotteryPrizeController
 * @Date 2025/7/31
 * @module ActivityLotteryPrizeController
 * @author: su
 */
@Slf4j
@RestController
@RequestMapping("/admin/user-lottery/record")
public class AdminLotteryRecordController {

    @Autowired
    private UserLotteryRecordService userLotteryRecordService;


    /**
     * 获取用户抽奖记录
     *
     * @param paramVo
     * @return
     */
    @PostMapping("/find-page/user")
    public QResult<Page<UserLotteryRecord>> findPageByUser(@RequestBody UserLotteryRecordQueryVo paramVo) {
        log.info("findPageByUser paramVo:{}", paramVo);
        paramVo.setRecordType(UserLotteryRecord.RECORD_TYPE_USER);
        Page<UserLotteryRecord> byPage = userLotteryRecordService.findByPage(paramVo);
        return QResult.success(byPage);
    }

    /**
     * 获取系统抽奖记录
     *
     * @param paramVo
     * @return
     */
    @PostMapping("/find-page/system")
    public QResult<Page<UserLotteryRecord>> findPageBySystem(@RequestBody UserLotteryRecordQueryVo paramVo) {
        log.info("findPageBySystem paramVo:{}", paramVo);
        paramVo.setRecordType(UserLotteryRecord.RECORD_TYPE_SYSTEM);
        Page<UserLotteryRecord> byPage = userLotteryRecordService.findByPage(paramVo);
        return QResult.success(byPage);
    }

    /**
     * 保存抽奖记录
     *
     * @return
     */
    @PostMapping("/save")
    public QResult save(@RequestBody UserLotteryRecord userLotteryRecord) {
        log.info("save userLotteryRecord:{}", userLotteryRecord);
        userLotteryRecordService.save(userLotteryRecord);
        return QResult.success(true);
    }

    /**
     * 修改记录领取状态
     *
     * @return
     */
    @PostMapping("/updateStatus")
    public QResult updateStatus(@RequestBody UserLotteryRecord userLotteryRecord) {
        log.info("updateStatus userLotteryRecord:{}", userLotteryRecord);
        userLotteryRecordService.updateRecordStatus(userLotteryRecord.getId(), userLotteryRecord.getStatus());
        return QResult.success(true);
    }


    /**
     * 删除抽奖记录
     *
     * @param userLotteryRecord
     * @return
     */
    @PostMapping("/delete")
    public QResult delete(@RequestBody UserLotteryRecord userLotteryRecord) {
        log.info("delete id:{}", userLotteryRecord.getId());
        userLotteryRecordService.deleteById(userLotteryRecord.getId());
        return QResult.success(true);
    }

    /**
     * 导入系统配置 抽奖记录
     *
     * @param file
     * @return
     */
    @PostMapping("/import/system")
    public QResult<List<UserLotteryRecord>> importSystem(@RequestParam String activityId, @RequestPart("file") MultipartFile file) {
        List<UserLotteryRecordExcel> excelDataList = ExcelUtils.readExcel(file, UserLotteryRecordExcel.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return QResult.fail("未解析出数据");
        }
        for (UserLotteryRecordExcel userLotteryRecordExcel : excelDataList) {
            userLotteryRecordExcel.setActivityId(activityId);
        }
        userLotteryRecordService.importSystem(excelDataList);
        return QResult.success(null);
    }
}
